package controllers

//微信公众平台消息处理

import (
	"crypto/sha1"
	"encoding/xml"
	"fmt"
	"github.com/robfig/revel"
	"io"
	"io/ioutil"
	"kaibb/app/models"
	"sort"
)

type Weixin struct {
	*revel.Controller
}

var wtoken = "6uBuKPYKVrTe"

//微信接入认证
func (this Weixin) Auth(signature, timestamp, nonce, echostr string) revel.Result {
	revel.INFO.Printf("signature:%s,timestamp:%s,noce:%s,echostr:%s", signature, timestamp, nonce, echostr)
	str := []string{wtoken, timestamp, nonce}
	//字典排序
	sort.Strings(str)
	temStr := str[0] + str[1] + str[2]
	//sha1加密
	sh := sha1.New()
	io.WriteString(sh, temStr)
	sig := fmt.Sprintf("%x", sh.Sum(nil))
	revel.INFO.Printf("计算签名:%s", sig)
	//如果签名正确则返回echostr,否则返回错误
	if signature == sig {
		return this.RenderText(echostr)
	} else {
		return this.RenderText("Error Request")
	}
}

//处理微信发送过来的消息
func (this Weixin) Process() revel.Result {
	data, err := ioutil.ReadAll(this.Request.Body)
	f := &models.Message{}
	err = xml.Unmarshal(data, &f)
	if err != nil {

	}
	revel.INFO.Printf("event Msg :", f)
	var to *models.Message
	switch f.MsgType {
	//文本消息
	case models.TEXT:
		to = f.ToTextMsg("测试文本消息: " + f.Content)
		//图片消息
	case models.PIC:
		to = f.TOWxPicMsg(f.PicUrl)
	case models.VIDEO:
	case models.VOICE:
		to = f.ToMusicMsg("语音消息", "测试语音", f.Url, f.Url, "http://img.9eye.cn/artist/585.jpg")
	case models.AUDIO:
		to = f.ToMusicMsg("语音消息", "测试语音", f.Url, f.Url, "http://img.9eye.cn/artist/585.jpg")
	case models.NEW:
	case models.LOCATION:
	case models.MUSIC:
		to = f.ToMusicMsg("语音消息", "测试语音", "http://kaibb.com/public/temp/%E7%94%9F%E6%9D%A5%E5%BD%B7%E5%BE%A8.mp3", "http://kaibb.com/public/temp/%E7%94%9F%E6%9D%A5%E5%BD%B7%E5%BE%A8.mp3", "http://img.9eye.cn/artist/585.jpg")
	case models.EVENT:
		switch f.Event {
		case models.SUBSRIBE:
			//关注
			if f.EventKey == "" {
				item := []models.Item{}
				item = append(item, models.Item{Title: "易信公众开发平台示例(GO语言)", Description: "公众平台消息接口为开发者提供了与用户进行消息交互的能力。对于成功接入消息接口的易信公众帐号，当用户发消息给公众帐号，易信公众平台服务器会使用http请求对接入的网址进行消息推送，第三方服务器回复特定结构的响应包，可达到回复消息的目的。", PicUrl: "http://img.9eye.cn/artist/100.jpg", Url: "http://kaibb.com/view/52c13fe4cf48c35972000004"})
				to = f.ToNewsMsg(item)
			} else {

			}
			//退订事件
		case models.UNSUBSCRIBE:
			//扫描二维码事件
		case models.SCAN:
			//点击菜单事件
		case models.CLICK:
			switch f.EventKey {
			case models.TEXT:
				to = f.ToTextMsg("测试菜单文本消息: ")
			case models.PIC:
				to = f.TOWxPicMsg(f.PicUrl)
			case models.NEW:
				item := []models.Item{}
				item = append(item, models.Item{Title: "易信公众开发平台示例(GO语言)", Description: "公众平台消息接口为开发者提供了与用户进行消息交互的能力。对于成功接入消息接口的易信公众帐号，当用户发消息给公众帐号，易信公众平台服务器会使用http请求对接入的网址进行消息推送，第三方服务器回复特定结构的响应包，可达到回复消息的目的。", PicUrl: "http://img.9eye.cn/artist/100.jpg", Url: "http://kaibb.com/view/52c13fe4cf48c35972000004"})
				to = f.ToNewsMsg(item)
			case models.MUSIC:
				to = f.ToMusicMsg("语音消息", "测试语音", "http://kaibb.com/public/temp/%E7%94%9F%E6%9D%A5%E5%BD%B7%E5%BE%A8.mp3", "http://kaibb.com/public/temp/%E7%94%9F%E6%9D%A5%E5%BD%B7%E5%BE%A8.mp3", "http://img.9eye.cn/artist/585.jpg")
			case "我爱你":
				to = f.ToTextMsg("我也爱你")
			default:
				to = f.ToTextMsg("hello 易信")
			}

		}
	default:
		to = f.ToTextMsg("其它事件")

	}
	revel.INFO.Printf("To Msg :", to)
	return this.RenderXml(to)

}
